package com.joe.blockview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.joe.blockview.model.Plain;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class PlainAdapter extends RecyclerView.Adapter<PlainAdapter.ViewHolder> {
    private List<Plain> plainList;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public PlainAdapter(Context context, List<Plain> plains, OnItemClickListener onItemClickListener) {
        this.plainList = plains;
        this.context = context;
        this.onItemClickListener = onItemClickListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.layout_plain_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.tv.setText(plainList.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(v, plainList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return plainList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.plain_item_tv);
        }
    }
}
